//
//  CS_Page_GpsAddressView.m
//  Pods
//
//  Created by 网新中研 on 17/3/1.
//
//

#import "CS_Page_GpsAddressView.h"

@interface CS_Page_GpsAddressView()

@property (nonatomic, strong) NSString *title;

@property (nonatomic, strong) UIButton *manualMarkButton;

@property (nonatomic, strong) UIButton *autoMarkButton;

@property (nonatomic, strong) UILabel *latitudeLabel;

@property (nonatomic, strong) UILabel *longitudeLabel;

@property (nonatomic, strong) UIViewController *superViewController;

@property (nonatomic, strong) UIView *seperatorLine;

@property (nonatomic, copy) OnAutoLocationClickBlock onAutoLocationClickHandler;

@property (nonatomic, copy) OnManualLocationClickBlock onManualLocationClickHandler;

@end

#define CELL_BUTTON_WIDTH 80.0
#define CELL_BUTTON_HEIGHT 25.0

@implementation CS_Page_GpsAddressView

+(instancetype)page_gpsAddressView:(NSString *)title longitude:(NSString *)longitude latitude:(NSString *)latitude onAutoLocationClickHandler:(OnAutoLocationClickBlock)autonLocationClickHandler onManualLocationClickHandler:(OnManualLocationClickBlock)manualLocationClickHandler{
    
    CS_Page_GpsAddressView *gpsAddressView = [CS_Page_GpsAddressView new];
    gpsAddressView.longitude = longitude;
    gpsAddressView.latitude = latitude;
    gpsAddressView.title = title;
    gpsAddressView.onAutoLocationClickHandler = autonLocationClickHandler;
    gpsAddressView.onManualLocationClickHandler = manualLocationClickHandler;
    
    return gpsAddressView;
    
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    CGFloat localPrimary = 0.0;
    
    
    //标题
    CGFloat estimateDescriptionHeight = [self estimateHeight:self.title font:self.descriptionLabel.font width:CELL_TITLE_WIDTH];
    CGFloat finalDescriptionHeight = estimateDescriptionHeight > CELL_LABLE_HEIGHT? estimateDescriptionHeight : CELL_LABLE_HEIGHT;
//    self.descriptionLabel.frame = CGRectMake(0, primaryY + CELL_TOP_MARGIN, CELL_TITLE_WIDTH, finalDescriptionHeight);
//    self.descriptionLabel.text = self.title;
//    [superView addSubview:self.descriptionLabel];
    
    //纬度
    self.latitudeLabel = [UILabel new];
    self.latitudeLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE_1];
    self.latitudeLabel.textColor = COLOR_TEXT_2;
    self.latitudeLabel.text = [NSString stringWithFormat:@"纬度：%@", self.latitude == nil?@"未获取当前位置":self.latitude];
    self.latitudeLabel.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, CELL_TOP_MARGIN, kWindowW - CELL_TITLE_WIDTH, 20);
    [superView addSubview:self.latitudeLabel];
    
    //经度
    self.longitudeLabel = [UILabel new];
    self.longitudeLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE_1];
    self.longitudeLabel.textColor = COLOR_TEXT_2;
    self.longitudeLabel.text = [NSString stringWithFormat:@"经度：%@", self.longitude == nil?@"未获取当前位置":self.longitude];
    self.longitudeLabel.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, 30 + CELL_TOP_MARGIN + 3, kWindowW - CELL_TITLE_WIDTH, 20);
    [superView addSubview:self.longitudeLabel];
    
    //自动获取
    self.autoMarkButton.frame = CGRectMake(kWindowW - CELL_EDIT_RIGHT_MARGIN - CELL_BUTTON_WIDTH, CELL_TOP_MARGIN - 5, CELL_BUTTON_WIDTH, CELL_BUTTON_HEIGHT);
    STYLE_BUTTON_LOCATION(self.autoMarkButton);
    
    //手动获取
    self.manualMarkButton.frame = CGRectMake(kWindowW - CELL_EDIT_RIGHT_MARGIN - CELL_BUTTON_WIDTH, CELL_TOP_MARGIN + 30, CELL_BUTTON_WIDTH, CELL_BUTTON_HEIGHT);
    STYLE_BUTTON_LOCATION(self.manualMarkButton);
    
    localPrimary = 20 + CELL_TOP_MARGIN + CELL_BOTTOM_MARGIN + CELL_TOP_MARGIN;
    
    self.seperatorLine.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, localPrimary + CELL_TOP_MARGIN + CELL_BOTTOM_MARGIN, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN, CELL_SEPERATOR_LINE_HEIGHT);
    
    [self addSubview:self.latitudeLabel];
    [self addSubview:self.longitudeLabel];
    [self addSubview:self.autoMarkButton];
    [self addSubview:self.manualMarkButton];
    [self addSubview:self.seperatorLine];
    
    self.frame = CGRectMake(0, primaryY, kWindowW, localPrimary + CELL_TOP_MARGIN + CELL_BOTTOM_MARGIN + CELL_SEPERATOR_LINE_HEIGHT);
    [superView addSubview:self];
    
    return [super layoutView:superView primaryY:primaryY + localPrimary + CELL_TOP_MARGIN + CELL_BOTTOM_MARGIN + CELL_SEPERATOR_LINE_HEIGHT];
}

-(void)getManualLocation{
    
    if (self.onManualLocationClickHandler) {
        self.onManualLocationClickHandler(self);
    }
    
}

-(void)getAutoLoaction{
    
    if (self.onAutoLocationClickHandler) {
        self.onAutoLocationClickHandler(self);
    }
    
}

-(void)setLongitude:(NSString *)longitude{
    _longitude = longitude;
    self.longitudeLabel.text = [NSString stringWithFormat:@"经度：%@", self.longitude == nil?@"未获取当前位置":self.longitude];
}

-(void)setLatitude:(NSString *)latitude{
    _latitude = latitude;
    self.latitudeLabel.text = [NSString stringWithFormat:@"纬度：%@", self.latitude == nil?@"未获取当前位置":self.latitude];
}


- (UIButton *)manualMarkButton {
    if(_manualMarkButton == nil) {
        _manualMarkButton = [[UIButton alloc] init];
        [_manualMarkButton setTitle:@"手动获取" forState:UIControlStateNormal];
        STYLE_BUTTON_ROUND(_manualMarkButton);
        [_manualMarkButton addTarget:self action:@selector(getManualLocation) forControlEvents:UIControlEventTouchUpInside];
        _manualMarkButton.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE_2];
        _manualMarkButton.backgroundColor = [UIColor redColor];
    }
    return _manualMarkButton;
}

- (UIButton *)autoMarkButton {
    if(_autoMarkButton == nil) {
        _autoMarkButton = [[UIButton alloc] init];
        [_autoMarkButton setTitle:@"自动标注" forState:UIControlStateNormal];
        STYLE_BUTTON_ROUND(_autoMarkButton);
        [_autoMarkButton addTarget:self action:@selector(getAutoLoaction) forControlEvents:UIControlEventTouchUpInside];
        _autoMarkButton.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE_2];
    }
    return _autoMarkButton;
}

-(void)reset{
    self.longitude = @"";
    self.latitude = @"";
    self.longitudeLabel.text = @"";
    self.latitudeLabel.text = @"";
}

- (UIView *)seperatorLine {
    if(_seperatorLine == nil) {
        _seperatorLine = [[UIView alloc] init];
        STYLE_SEPERATOR_LINE(_seperatorLine);
    }
    return _seperatorLine;
}

@end
